package com.example.rdcproject.ving.notes.dao;

import com.example.rdcproject.ving.notes.entity.Word;
import com.example.rdcproject.ving.notes.service.ConnectionPool;
import com.example.rdcproject.ving.notes.service.ThreadPoolService;
import com.example.rdcproject.ving.notes.util.DBUtils;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WordBookDao implements Runnable{

    private Connection connection;
     private  ConnectionPool connectionPool;
    private PreparedStatement statement;

    ThreadPoolService threadPoolService = new ThreadPoolService();

    @Override
    public void run() {

        threadPoolService.execute(this);
    }



    //删除单词
    public boolean deleteWord(String wordBookName,String word) {

        threadPoolService.execute(this);

        ConnectionPool connectionPool = ConnectionPool.getInstance();
        try  {

            connection = connectionPool.getConnection();

            String sql = "delete from "+wordBookName+" where word =?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,word);
       int  result =  statement.executeUpdate();
            threadPoolService.shutdown();
            if (result>0){
                return  true;
            }else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            connectionPool.releaseConnection(connection);
            try {
                DBUtils.close(statement);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }

    //修改单词
    public  void updateWord(String bookName,String word,String meaning) throws SQLException {

        threadPoolService.execute(this);
        ConnectionPool connectionPool = ConnectionPool.getInstance();
        try{
             connection = DBUtils.getConnection();

             String sql = "update "+bookName+" set meaning =? where word =?";
             statement=connection.prepareStatement(sql);

             statement.setString(1,meaning);
             statement.setString(2,word);
             statement.executeUpdate();
         }catch (Exception e){
             e.printStackTrace();
         }finally {
            DBUtils.close(statement);
            connectionPool.releaseConnection(connection);

         }

    }


    //搜索单词
    public  void searchWord(String bookName,String word)  {

        threadPoolService.execute(this);
        ConnectionPool connectionPool = ConnectionPool.getInstance();
        Map<String,String> map = new HashMap<>();
        try {
            connection = connectionPool.getConnection();

            String sql = "select * from "+bookName+" where word like ?";
            statement=connection.prepareStatement(sql);
            //模糊查询
            statement.setString(1,word+"%");

            ResultSet rs = statement.executeQuery();
            while (rs.next()){
                 map.put(rs.getString("word"),rs.getString("meaning"));
            }
            //遍历所有键值对象的集合
            Set<Map.Entry<String,String>> entrySet = map.entrySet();
            //遍历键值对对象集合
            for (Map.Entry<String, String> me : entrySet) {
                //根据键值对对象获取键和值
                String key = me.getKey();
                String value = me.getValue();
                System.out.println(key+"："+value);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("搜索失败！");
        }finally {
            try {
                DBUtils.close(statement);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            connectionPool.releaseConnection(connection);

        }

    }
    public void closeThreadPool()
    {
        threadPoolService.shutdown();
    }

}
